﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MS_UI;
using MS_Entity;
using System.Data.SqlTypes;
using DAL;



namespace MS_UI
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
            //透明背景色,视频2-2
           
        }

        private void btn_Login_Click(object sender, EventArgs e)
        {
            //判断是否选中匿名登录
            if (this.chk_UsePwd.Checked)
            {
                this.DialogResult = DialogResult.OK;

                CommonMethods.objAdmins.LoginName = "None";

            }
            else
            {

                if (this.txt_UserID.Text.Trim().Length == 0)
                {
                    MessageBox.Show("请输入用户名");
                    //把焦点放在用户名框
                 this.txt_UserID.Focus();

                    return;
                
                }

                if (DataValidate.IsInteger(this.txt_UserID.Text.Trim()))
                {

                    MessageBox.Show("用户名必须为正整数");
                    //把焦点放在用户名框
                    this.txt_UserID.Focus();

                }

                if (this.txt_Pwd.Text.Trim().Length == 0)
                {

                    MessageBox.Show("请输入登录密码");
                    //把焦点放在用户名框
                    this.txt_Pwd.Focus();

                    return;

                }


                //封装对象
                SysAdmins objAdmins = new SysAdmins()
                {
                    LoginID = Convert.ToInt32(this.txt_UserID.Text.Trim()),
                    LoginPwd = this.txt_Pwd.Text.Trim()

                };
                if (AdminLogin(objAdmins) == null)
                {

                    MessageBox.Show("用户名密码错误");

                }

                else
                {
                    this.DialogResult = DialogResult.OK;
                    CommonMethods.objAdmins=objAdmins;
                }

            }

        }


        public SysAdmins AdminLogin(SysAdmins objAdmin)
        {

            string sql = "Select LoginName,Role from sysAdmins where LoginId={0} and loginPwd{1}";

            sql = string.Format(sql, objAdmin.LoginID, objAdmin.LoginPwd);
             DataSet ds=SQLHelper.GetDataSet(sql);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                objAdmin.LoginName = ds.Tables[0].Rows[0]["loginName"].ToString();
                objAdmin.Role = Convert.ToInt32(ds.Tables[0].Rows[0]["Role"]);
            
            return objAdmin;

            
            }
            return objAdmin;

        }

        private void txt_UserID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {

                this.btn_Login_Click(null, null);
            
            }
        }

        private void txt_Pwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                this.btn_Login_Click(null, null);

            }
        }

    }
}

